<template>
    <rx-dialog ref="rxDialog" :showok="false" btnalign="right" order="top" @cancel="cancel">
        <div slot="toolbar">
            <a-button v-if="currentName!=0" class="toolbuton" type="primary" @click="onBefore">{{i18n('button.onBefore','上一步')}}</a-button>
            <a-button v-if="currentName!=3" class="toolbuton" type="primary" @click="onNext">{{i18n('button.onNext','下一步')}}</a-button>
            <a-button type="primary" @click="onSave()">{{$ti18('rxForm.saveBtn','保存')}}</a-button>
            <a-button v-if="currentName==3" class="toolbuton" type="primary" @click="codeGen">{{i18n('list.codeGen','生成代码')}}</a-button>
        </div>
        <rx-layout>
            <div slot="header">
                <a-steps :current="currentName">
                    <a-step v-for="item in steps" :key="item.title" :disabled="item.disabled" :title="item.title" />
                </a-steps>
            </div>
            <div slot="center">
                <comment ref="current" :is="currentView" :formData="formData" @changeForm="changeForm"></comment>
            </div>
        </rx-layout>
    </rx-dialog>
</template>
<script>
    import FormCodegenBusinessApi from "@/api/form/codegen/formCodegenBusiness";
    import {RxDialog,Util,App} from "@lowcode/jpaas-common-lib";
    import FormCodegenBusinessEdit1 from "./FormCodegenBusinessEdit1.vue";
    import FormCodegenBusinessEdit2 from "./FormCodegenBusinessEdit2.vue";
    import FormCodegenBusinessEdit3 from "./FormCodegenBusinessEdit3.vue";
    import FormCodegenBusinessEdit4 from "./FormCodegenBusinessEdit4.vue";

    export default {
        name: 'FormCodegenBusinessEdit',
        props:["pkId","layerid","destroy"],
        mixins:[App],
        components: {
            RxDialog,
            FormCodegenBusinessEdit1,
            FormCodegenBusinessEdit2,
            FormCodegenBusinessEdit3,
            FormCodegenBusinessEdit4
        },
        data() {
            return {
                currentName: 0,
                edit1: FormCodegenBusinessEdit1,
                edit2: FormCodegenBusinessEdit2,
                edit3: FormCodegenBusinessEdit3,
                edit4: FormCodegenBusinessEdit4,
                currentView: FormCodegenBusinessEdit1,
                steps: [
                    {key:'edit1',title:this.i18n('steps.edit1','基本信息'),disabled:true},
                    {key:'edit2',title:this.i18n('steps.edit2','数据列表'),disabled:true},
                    {key:'edit3',title:this.i18n('steps.edit3','主子表'),disabled:true},
                    {key:'edit4',title:this.i18n('steps.edit4','生成文件'),disabled:true}
                ],
                formData:{
                    source:"businessModel",
                    status:"NO"
                }
            }
        },
        created(){
            this.get();
        },
        methods: {
            i18n(name,text,key){
                return this.$ti18(name,text,"formCodegenBusiness",key);
            },
            codeGen(){
                this.onSave(function(formData){
                    FormCodegenBusinessApi.genCode(formData.id);
                })
            },
            changeForm(valJson){
                this.formData=valJson;
            },
            onNext(vm){
                vm.loading =false;
                let self_=this;
                //如果是第四步，则走上一步
                if(self_.currentName==0){
                    function callback(err) {
                        if(err){
                            vm.loading =false;
                            return;
                        }
                        self_.currentView=self_.edit2;
                        self_.currentName=1;
                    }
                    self_.currentView.methods.handleSubmit(self_,self_.$refs.current,callback);
                }else if(self_.currentName==1){
                    function callback(err) {
                        if(err){
                            vm.loading =false;
                            return;
                        }
                        self_.currentView=self_.edit3;
                        self_.currentName=2;
                    }
                    self_.currentView.methods.handleSubmit(self_,self_.$refs.current,callback);
                }else {
                    function callback(err) {
                        if(err){
                            vm.loading =false;
                            return;
                        }
                        self_.currentView=self_.edit4;
                        self_.currentName=3;
                    }
                    self_.currentView.methods.handleSubmit(self_,self_.$refs.current,callback);
                }
            },
            onBefore(){
                if(this.currentName==2){
                    this.currentView=this.edit2;
                    this.currentName=1;
                }else if(this.currentName==3){
                    this.currentView.methods.handleBefore(this,this.$refs.current);
                    this.currentView=this.edit3;
                    this.currentName=2;
                }else {
                    this.currentView=this.edit1;
                    this.currentName=0;
                }
            },
            cancel(){
                Util.closeWindow(this,"ok");
            },
            get() {
                var self_ = this;
                if (self_.pkId) {
                    FormCodegenBusinessApi.get(this.pkId).then(res => {
                        self_.formData = res.data;
                    });
                }
            },
            onSave(callFunction) {
                let self_=this;
                function callback(err) {
                    if(err) {
                        return;
                    }
                    let formData=Util.deepClone(self_.formData);
                    let settingJson=JSON.parse(formData.settingJson);
                    //删除tmpVars属性,不保存
                    delete settingJson.tmpVars;
                    formData.settingJson=JSON.stringify(settingJson);
                    if(formData.status!="YES") {
                        formData.status = self_.currentName == 3 ? "YES" : "NO";
                    }
                    let appId=self_.curApp.appId;
                    if(appId && !self_.pkId){
                        formData.appId=appId;
                    }
                    FormCodegenBusinessApi.save(formData).then(res=>{
                        if(res.success) {
                            self_.formData.id = res.data.id;
                            if(callFunction){
                                callFunction(self_.formData);
                            }
                        }
                    });
                }
                this.currentView.methods.handleSubmit(this,this.$refs.current,callback);
            }
        }
    }
</script>
<style scoped>

</style>
